/**
 * 
 * @param {number} n 随机长度
 * @returns 
 */
export const randomString = (n: number) => {
  let str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let tmp = '',
    i = 0,
    l = str.length;
  for (i = 0; i < n; i++) {
    tmp += str.charAt(Math.floor(Math.random() * l));
  }
  return tmp;
}
/**
 * 下载
 */
export const handleDownloadByName = (url:string, downLoadFileName:string, openNewWindow:boolean) => {
  // url = url.replace("http://121.89.192.223:8888", "http://121.89.192.223")
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  link.setAttribute(
      'download',
      downLoadFileName ? downLoadFileName : "文件导出-未命名"
  )
  console.log("link---->", link)
  if (openNewWindow) {
      link.target = "_new";
  }
  document.body.appendChild(link)
  link.click()
}
/**
 * 递归方法
 * @param {Arraay} list 数组 
 * @param {Number} pid 父级Id
 * @param {Object} params 可配置参数 
 * @returns 
 */
export const generateTree = (list: Array<any>, pid: number, params: any) => {
	if (!Array.isArray(list)) {
		new Error('only Array');
		return list;
	}
	return list.reduce((pre, cur) => {
		if (cur[params.pidName] === pid) {
			const children = generateTree(list, cur[params.idName], params);
			if (children.length) {
				cur[params.childrenName] = children;
			}
			return [...pre, cur];
		}
		return pre;
	}, []);
};
